package com.yoomiito.app.ui.order;

import android.content.Context;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.yoomiito.app.R;
import com.yoomiito.app.widget.dialog.BaseDialog;
import com.yoomiito.app.widget.SelectView;

import java.util.ArrayList;
import java.util.List;

public class UseCouponDialog extends BaseDialog implements View.OnClickListener {

    private OnSelectListener mOnSelectListener;
    private int mPosition = 0;
    private boolean isUseCoupon = true;
    private List<String> mCouponList;
    private RcyAdapter rcyAdapter;

    public UseCouponDialog(@NonNull Context context) {
        this(context,0);
    }

    public UseCouponDialog(@NonNull Context context, int themeResId) {
        super(context, R.style.dialog_1);

    }

    protected UseCouponDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    @Override
    public View setView() {
        setmGravity(Gravity.BOTTOM);
        setWidthPercent(1);
        View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_select_coupon, null);
        RecyclerView rcy = view.findViewById(R.id.dialog_use_coupon_rcy);
        view.findViewById(R.id.dialog_use_coupon_btn).setOnClickListener(this);
        rcy.setLayoutManager(new LinearLayoutManager(mContext,LinearLayoutManager.VERTICAL,false));
        mCouponList = new ArrayList<>();
        mCouponList.add("");
        mCouponList.add("");
        mCouponList.add("");
        rcyAdapter = new RcyAdapter(mCouponList);
        rcy.setAdapter(rcyAdapter);
        return view;
    }

    @Override
    public void onClick(View v) {
        if (mPosition==mCouponList.size()-1){
            isUseCoupon = false;
        }
        if (mOnSelectListener!=null){
            mOnSelectListener.onSelect(mPosition,isUseCoupon);
        }
    }

    public interface OnSelectListener{
        void onSelect(int position,boolean isUseCoupon);
    }

    public void setOnSelectListener(OnSelectListener onSelectListener) {
        mOnSelectListener = onSelectListener;
    }

    public void setCouponList(List<String> couponList) {
        mCouponList = couponList;
        rcyAdapter.setNewData(couponList);
    }

    private class RcyAdapter extends BaseQuickAdapter<String, BaseViewHolder> {


        public RcyAdapter(@Nullable List<String> data) {
            super(R.layout.item_coupon_use, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, String item) {
            TextView tv = helper.getView(R.id.item_coupon_content);
            tv.setText(item);
            SelectView sv =helper.getView(R.id.item_coupon_select_sv);
            if (mPosition==helper.getLayoutPosition()){
                sv.setSelect(true);
            }else {
                sv.setSelect(false);
            }
            sv.setClickListener(v -> {
                mPosition = helper.getLayoutPosition();
                notifyDataSetChanged();
            });
        }
    }
}
